package com.sharemarking.system.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.sharemarking.system.entity.User;

public interface UserMapper {
	@Select("SELECT * FROM sys_user WHERE id=#{id}")
	User get(int id);
	@Select("SELECT * FROM sys_user WHERE login_name=#{loginName}")
	User getByLoginName(String loginName);
	@Select("SELECT * FROM sys_user WHERE mobile=#{mobile} LIMIT 1")
	User getByMobile(String mobile);
	@Insert("INSERT INTO sys_user (name,login_name,password,email,mobile,enabled) "
			+ "VALUES (#{name},#{loginName},#{password},#{email},#{mobile},#{enabled}) ")
	@Options(useGeneratedKeys=true, keyProperty="id",keyColumn = "id")
	void insert(User user);
	@Insert("INSERT INTO sys_user_role VALUES (#{param1},#{param2})")
	void insertUserRole(int userId,int roleId);
	@Insert("INSERT INTO sys_user_permission VALUES (#{param1},#{param2}) ")
	void insertUserPermission(int userId,int permissionId);
	@Select("SELECT role_id FROM sys_user_role WHERE user_id=#{param1}")
	List<Integer> getRoleIdsByUserId(int userId);
	@Select("SELECT permission_id FROM sys_user_permission WHERE user_id=#{param1}")
	List<Integer> getPermissionIdsByUserId(int userId);
	@Update("UPDATE sys_user SET name=#{name},email=#{email},mobile=#{mobile},enabled=#{enabled} WHERE id=${id}")
	void updateInfo(User user);
	@Update("UPDATE sys_user SET password=#{password} WHERE id=#{id}")
	void updatePassword(User user);
	@Delete("DELETE FROM sys_user_permission WHERE user_id=#{param1} AND permission_id=#{param2}")
	void deleteUserPermission(int userId,int permissionId);
	@Delete("DELETE FROM sys_user_role WHERE user_id=#{param1} AND role_id=#{param2}")
	void deleteUserRole(int userId,int roleId);
	@Delete("DELETE FROM sys_user WHERE id=#{id}")
	void deleteUser(int id);
	@Delete("DELETE FROM sys_user_role WHERE user_id=#{userId}")
	void deleteUserAllRole(int userId);
	@Delete("DELETE FROM sys_user_permission WHERE user_id=#{userId} ")
	void deleteUserAllPermission(int userId);
	
	List<User> getPage(@Param("name")String name,@Param("pageIndex")int pageIndex,@Param("pageSize")int pageSize);
	int getPageTotal(@Param("name")String name);
}
